package de.slothsoft.commons.swing.util;

import java.awt.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class JFileChooserHelper {

	private List<FileFilter> fileFilters = new ArrayList<FileFilter>();
	private String forceExtension;
	private File lastDirectory;

	public File chooseFolder(Component parent) {
		return chooseFolder(parent, this.lastDirectory);
	}

	public File chooseFolder(Component parent, File currentDirectory) {
		JFileChooser chooser = new JFileChooser(currentDirectory);
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		addFileFilters(chooser, this.fileFilters);
		int answer = chooser.showOpenDialog(parent);
		if (answer == JFileChooser.APPROVE_OPTION) {
			File result = chooser.getSelectedFile();
			this.lastDirectory = chooser.getCurrentDirectory();
			return result;
		}
		return null;
	}

	public File chooseOpenFile(Component parent) {
		return chooseOpenFile(parent, this.lastDirectory);
	}

	public File chooseOpenFile(Component parent, File currentDirectory) {
		JFileChooser chooser = new JFileChooser(currentDirectory);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		addFileFilters(chooser, this.fileFilters);
		int answer = chooser.showOpenDialog(parent);
		if (answer == JFileChooser.APPROVE_OPTION) {
			File result = chooser.getSelectedFile();
			this.lastDirectory = chooser.getCurrentDirectory();
			return result;
		}
		return null;
	}

	public File chooseSaveFile(Component parent) {
		return chooseSaveFile(parent, this.lastDirectory);
	}

	public File chooseSaveFile(Component parent, File currentDirectory) {
		JFileChooser chooser = new JFileChooser(currentDirectory);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		addFileFilters(chooser, this.fileFilters);
		int answer = chooser.showSaveDialog(parent);
		if (answer == JFileChooser.APPROVE_OPTION) {
			File result = chooser.getSelectedFile();
			if (this.forceExtension != null) {
				File fileWithForcedExtension = forceExtension(result,
						this.forceExtension);

				if (!fileWithForcedExtension.exists()) {
					// now it is to late to ask the user to confirm that he
					// wants to delete the file - so just save the file without
					// extension
					// TODO: think about it
					result = fileWithForcedExtension;
				}
			}
			this.lastDirectory = chooser.getCurrentDirectory();
			return result;
		}
		return null;
	}

	public File forceExtension(File file, String extension) {
		if (file.getName().endsWith("." + extension)) {
			return file;
		}
		return new File(file.getPath() + "." + extension);
	}

	private void addFileFilters(JFileChooser chooser,
			List<FileFilter> fileFilterList) {
		for (FileFilter filter : fileFilterList) {
			chooser.addChoosableFileFilter(filter);
		}
	}

	public String getForceExtension() {
		return this.forceExtension;
	}

	public void setForceExtension(String forceExtension) {
		this.forceExtension = forceExtension;
	}

	public List<FileFilter> getFileFilters() {
		return this.fileFilters;
	}

	public void addFileFilter(FileFilter filter) {
		this.fileFilters.add(filter);
	}
}
